package org.example

object mal {
  def main(args: Array[String]): Unit = {
    def addNum = (x:Int) => x*2
    val list1 = List(100,200,300).map(addNum)
    val list2 = List(100,200,300).map(x =>  x*2)
    val list3 = List(100,200,300).map(_*2)
    println(list3)
    val s = "nba"
    println(s.reverse)
    val wordList = List("java","scala","cba")
    println(wordList.map(_.reverse))
    println(wordList.reverse)
    val score = Array(("张三",Array(5.5,6.6,7.7,8.8,8.0)),("李四",Array(6.0,6.5,7.0,7.5,9.0)))
    score.map(x => (x._1,x._2.max)).foreach(println)
    score.map(tp => (tp._1,tp._2.map(t =>if(t >= 8.0)1 else 0).sum)).foreach(println)
    score.map(tp => (tp._1,tp._2.map(t =>if(t < 6.0)1 else 0).sum)).foreach(println)
    score.map(tp => (tp._1,tp._2.count(t => t < 6.0))).foreach(println)
    score.map(sp => (sp._1,sp._2.sum / sp._2.length)).foreach(println)
    // filter过滤 类型和元素不变
    val arr1 =Array(1,2,3,4,5,6)
    arr1.filter(t => if(t % 2== 0)true else false).foreach(println)
    arr1.filter(t => t % 2== 0).foreach(println)
    arr1.filter(_ % 2 == 1).foreach(println)
    // 练习3
    val arr2 = Array("I", "love", "you")
    arr2.filter(word => word.reverse == word).foreach(println)
//flatMap扁平映射
    val wordFlat = List("I am here","you are smart","I love scala")
    val wordSplit = wordFlat.map(_.split(""))
    val wordFlatten = wordSplit.flatten
    println(wordFlatten)
    val flatmapList = wordFlat.flatMap(_.split(""))
    println(flatmapList)
//groupBy分组
    println(flatmapList.groupBy(_.charAt(0)).toList)
    val wordSam = flatmapList.groupBy(word => word)
    println(wordSam)
    val countMap = wordSam.map(tp =>(tp._1,tp._2.length))
    println(countMap)
    val countList = countMap.toList
    println(countList)
    val sortList = countList.sortWith(_._2 > _._2)
    println(countList)
    val takeList = countList.take(2)
    println(takeList)
  }
}
